﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System.Collections.Generic;
using AAEngine.Engine.Debug;

namespace AAEngine.Engine.Core
{
    /// <summary>
    /// Set of AAObjects. A AAObject may be in many sets; sets do not destroy     
    /// their contained objects when they are destroyed. Sets automatically     
    /// remove Destroy()'ed objects. 
    /// </summary>
    public class AASet : AAObject
    {
        protected List<AAObject> _items = new List<AAObject>();

        /// <summary>
        /// Return the AAObject at the specified index.
        /// </summary>
        /// <param name="index">The index of the object to return.</param>
        /// <returns>The object at the given index or null if the index is out of range.</returns>
        public AAObject this[int index]
        {
            get
            {
                if (_items == null)
                {
                    return null;
                }

                if (index < 0 || index >= _items.Count)
                {
                    return null;
                }
                return _items[index];
            }
        }

        /// <summary>
        /// How many AAObjects are in this set?
        /// </summary>
        public int Length
        {
            get
            {
                return _items == null ? 0 : _items.Count;
            }
        }

        public bool Contains(AAObject item)
        {
            if (_items == null)
            {
                throw new AAException("Accessing Destroy()'ed set.");
            }

            return _items.Contains(item);
        }

        public bool Add(AAObject item)
        {
            // Can't add ourselves to ourselves.
            if (item == this)
            {
                return false;
            }

            if (_items == null)
            {
                throw new AAException("Accessing Destroy()'ed set.");
            }

            // Was the item present?
            if (_items.Contains(item))
            {
                return false;
            }

            // No, add it.
            ((AAObject)item).NoteInSet(this);
            _items.Add(item);
            return true;
        }

        public bool Remove(AAObject item)
        {
            // Can't remove ourselves from ourselves.
            if (item == this)
            {
                return false;
            }

            if (_items == null)
            {
                //throw new PBException("Accessing Destroy()'ed set.");
                Logger.Warn(this, "Remove", "Removed item from dead PBSet.");
                ((AAObject)item).NoteOutOfSet(this);
                return true;
            }

            // Was the item present?
            if (!_items.Contains(item))
            {
                return false;
            }

            ((AAObject)item).NoteOutOfSet(this);
            _items.Remove(this);
            return true;
        }

        /// <summary>
        /// Destroy all the objects in this set, but do not delete the group.
        /// </summary>
        public void Clear()
        {
            // Delete the items we own.
            while (_items.Count > 0)
            {
                (_items.Pop()).Destroy();
            }
        }

        public override void Destroy()
        {
            if (_items == null)
            {
                throw new AAException("Accessing Destroy()'ed set.");
            }

            // Delete everything.
            Clear();

            _items = null;

            // Pass control up.
            base.Destroy();
        }
    }
}
